home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / resources / templates / dynamic.js < prev    next >
Encoding:
Text File  |  2007-11-12  |  9.2 KB  |  297 lines

  1. <script type="text/javascript">
  2. <!-- // Protect from our XML parser, which doesn't know to protect <script>
  3.  
  4. ///////////////////////////////////////////////////////////////////////////////
  5. //// For use on your page                                                  ////
  6. ///////////////////////////////////////////////////////////////////////////////
  7.  
  8. function getDTVPlatform() {
  9.     var elt = document.getElementsByTagName("html")[0];
  10.     return elt.getAttribute('dtvPlatform');
  11. }
  12.  
  13. function loadURL(url) {
  14.     try {
  15.         document.location.href = url;
  16.     } catch (e) {
  17.         // This may happen if the backend decides to handle the url load
  18.         // itself.
  19.     }
  20. }
  21.  
  22. // For calling from page Javascript: Cause a URL to be loaded. The
  23. // assumption is that the application will notice, abort the load, and
  24. // take some action based on the URL.
  25. function eventURL(url) {
  26.     if (typeof(window.frontend) == 'undefined') {
  27.     // Generic strategy: trigger a load, and hope the application
  28.     // catches it and cancels it without creating a race
  29.     // condition.
  30.         loadURL(url)
  31.     } else {
  32.     // OS X WebKit (KHTML) strategy: pass in an Objective C object
  33.     // through the window object and call a method on it.
  34.     window.frontend.eventURL(url);
  35.     }
  36.  
  37.     return false;
  38. }
  39.  
  40. // Calls eventURL, then calls event.stopPropagation() and
  41. // event.preventDefault() so that the event chain is stopped.
  42. function eventURLAndStop(url, event) {
  43.   eventURL(url);
  44.   event.stopPropagation();
  45.   event.preventDefault();
  46. }
  47.  
  48. function recommendItem(title, url) {
  49.     loadURL('http://www.videobomb.com/index/democracyemail?url=' + 
  50.             url + '&title=' + title);
  51.     return false;
  52. }
  53.  
  54. function recommendChannel(title, url) {
  55.     // See also app.py if changing this URL
  56.     loadURL('http://www.videobomb.com/democracy_channel/email_friend' +
  57.         '?url=' + url + '&title=' + title);
  58.     return false;
  59. }
  60.  
  61. // Start the video player. The playlist will be the items in the view
  62. // named by viewName. If firstItemId is the id of an item in the view,
  63. // playback will start on that item; otherwise playback will start on
  64. // the first item.
  65. function playViewNamed(viewName, firstItemId) {
  66.     url = 'action:playViewNamed?';
  67.     url = url + 'viewName=' + URLencode(viewName);
  68.     url = url + '&firstItemId=' + URLencode(firstItemId);
  69.     eventURL(url);
  70.     return false;
  71. }
  72.  
  73. // You can make 'incremental search' text boxes on your page that
  74. // effectively tie the text box to the 'parameter' argument of setViewFilter,
  75. // with the other argumens fixed. To do this, add these two attributes to
  76. // the text box:
  77. //   onfocus="startEditSearch(this)"
  78. //   onblur="endEditFilter()"
  79. // replacing the arguments in parentheses with the desired strings.
  80. //
  81. // You'll also need to provide a updateSearchString function at the
  82. // top of your template to perform the actual update
  83.  
  84. var editSearchField = null;
  85. var editSearchOldValue = '';
  86. var editSearchTimer = null;
  87. var editSearchCallback = null;
  88.  
  89. function onSearchFocus(obj){
  90.   if (obj.getAttribute('searching') != '1') {
  91.     obj.value="";
  92.     obj.searching = '1';
  93.   }
  94.   startEditSearch(obj, null);
  95. }
  96.  
  97. function startEditSearch(obj, callback) {
  98.   editSearchOldValue = obj.value;
  99.  
  100.   editSearchField = obj;
  101.   editSearchCallback = callback;
  102.   editSearchTimerTick();
  103. }
  104.  
  105. function editSearchUpdate() {
  106.     value = editSearchField.value;
  107.     if (editSearchOldValue != value) {
  108.     url = 'action:setSearchString?searchString=' + URLencode(value);
  109.     eventURL(url);
  110.     editSearchOldValue = value;
  111.     if(editSearchCallback) editSearchCallback();
  112.     }
  113. }
  114.  
  115. function editSearchTimerTick() {
  116.     editSearchUpdate();
  117.     editSearchTimer = setTimeout(editSearchTimerTick, 50);
  118. }
  119.  
  120. function endEditSearch() {
  121.   clearTimeout(editSearchTimer);
  122.   editSearchUpdate();
  123. }
  124.  
  125. // Internal use: 'URL encode' the given string.
  126. function URLencode(str) {
  127.     return encodeURIComponent(str)
  128. }
  129.  
  130. function URLdecode(str) {
  131.   return decodeURIComponent(str)
  132. }
  133.  
  134. var currentSelectBoxMenu = null;
  135. function showSelectBoxMenu(id) {
  136.     document.getElementById(id).style.display = 'block';
  137.     currentSelectBoxMenu = id;
  138.     document.addEventListener('click', hideSelectBoxMenu, true)
  139. }
  140.  
  141. function hideSelectBoxMenu(event) {
  142.     document.getElementById(currentSelectBoxMenu).style.display = '';
  143.     currentSelectBoxMenu = null;
  144.     document.removeEventListener('click', hideSelectBoxMenu, true)
  145. }
  146.  
  147. ///////////////////////////////////////////////////////////////////////////////
  148. //// For calling by host templating code                                   ////
  149. ///////////////////////////////////////////////////////////////////////////////
  150.  
  151. // For calling by host templating code: Set CSS styles on the item
  152. // with the given ID to make it disappear.
  153. function hideItem(id) {
  154.     elt = document.getElementById(id);
  155.     elt.style.display = 'none';
  156.     forceRedisplay(elt);
  157. }
  158.  
  159. // For calling by host templating code: Set CSS styles on the item
  160. // with the given ID to make it visible if it was previously hidden.
  161. function showItem(id) {
  162.     elt = document.getElementById(id);
  163.     elt.style.display = '';
  164.     forceRedisplay(elt);
  165. }
  166.  
  167. // For calling by host templating code: Replace the item with the
  168. // given id with the element described by the proided XML.
  169. function changeItem(id, newXML) {
  170.     elt = document.getElementById(id);
  171.     r = document.createRange();
  172.     r.selectNode(elt);
  173.     frag = r.createContextualFragment(newXML);
  174.     elt.parentNode.replaceChild(frag, elt);
  175. }
  176.  
  177. // For calling by host templating code: Parse the XML in newXML into a
  178. // new element, and insert the new element immediately before the item
  179. // with the given id, such that the newly inserted item has the same
  180. // parent.
  181. function addItemBefore(newXML, id) {
  182.     elt = document.getElementById(id);
  183.     r = document.createRange();
  184.     r.selectNode(elt);
  185.     frag = r.createContextualFragment(newXML);
  186.     elt.parentNode.insertBefore(frag, elt);
  187. }    
  188.  
  189. // For calling by host templating code: Parse the XML in newXML into a
  190. // new element, and insert the new element as the final child of the
  191. // item with the given id.
  192. function addItemAtEnd(newXML, id) {
  193.     elt = document.getElementById(id);
  194.     r = document.createRange();
  195.     r.selectNode(elt);
  196.     frag = r.createContextualFragment(newXML);
  197.     elt.insertBefore(frag, null);
  198. }    
  199.  
  200. // For calling by host templating code: Remove the item with the given
  201. // id.
  202. function removeItem(id) {
  203.     elt = document.getElementById(id);
  204.     elt.parentNode.removeChild(elt);
  205. }    
  206.  
  207. // Internal use: Sometime if all you do is change the style on a node,
  208. // Safari doesn't update the view until your mouse is next over the
  209. // window. Force the issue by making a drastic change in the vicinity
  210. // of the given element and then reversing it.
  211. function forceRedisplay(elt) {
  212.     r = document.createRange();
  213.     r.selectNode(elt);
  214.     frag = r.extractContents();
  215.     r.insertNode(frag);
  216. }
  217.  
  218. function handleContextMenuSelect(event) {
  219.   if(event.button == 2) {
  220.     var area = event.currentTarget.getAttribute("selectArea");
  221.     var id = event.currentTarget.getAttribute("selectID");
  222.     var viewName = event.currentTarget.getAttribute("selectViewName");
  223.     var url = 'action:handleContextMenuSelect?id=' + id + '&area=' + area +
  224.               '&viewName=' + viewName;
  225.     eventURL(url);
  226.   }
  227.   return true;
  228. }
  229.  
  230. function handleSelect(event) {
  231.    if(event.target.tagName && event.target.tagName.toUpperCase() == 'A') {
  232.        // Either a link in the descrption, or a bomb/mailto/trash click
  233.        return true;
  234.     }
  235.     var id = event.currentTarget.getAttribute("selectID");
  236.     var viewName = event.currentTarget.getAttribute("selectViewName");
  237.     var area = event.currentTarget.getAttribute("selectArea");
  238.     var shiftKey = '0';
  239.     var ctrlKey = '0';
  240.     if(event.shiftKey) shiftKey = '1';
  241.     if(event.ctrlKey || event.metaKey) ctrlKey = '1';
  242.     eventURL('action:handleSelect?area=' + area + '&viewName=' + viewName + 
  243.     '&id=' + id + '&shiftDown=' + shiftKey + '&ctrlDown=' + ctrlKey);
  244.     return true;
  245. }
  246.  
  247. function handleDblClick(event, viewName, id) {
  248.    var target = event.target;
  249.    while (target != undefined && target.ondblclick === null && target.tagName.toUpperCase() != 'A') {
  250.        target = target.parentNode;
  251.    }
  252.  
  253.    if(target.tagName.toUpperCase() == 'A') {
  254.        // Either a link in the descrption, or a bomb/mailto/trash click
  255.        return true;
  256.    } else {
  257.        return eventURL('action:playViewNamed?viewName=' + viewName + 
  258.            '&firstItemId=' + id);
  259.    }
  260. }
  261.  
  262. function getKeyFromEvent(evt) {
  263.   var key = 0;
  264.   if (window.event)  {
  265.     key = evt.keyCode;
  266.   } else if (evt.which) {
  267.       key = evt.which;
  268.   }
  269.  
  270.   return key;
  271. }
  272.  
  273. function sendKeyToSearchBox(event) {
  274.   if(event.altKey || event.ctrlKey || event.metaKey ||
  275.       (event.target.tagName && event.target.tagName.toUpperCase() == 'INPUT'))
  276.       return true;
  277.   var key = getKeyFromEvent(event);
  278.   if ((key == 33) || (key == 34) || (key == 35) || (key == 36) || 
  279.       (key == 37) || (key == 38) || (key == 39) || (key == 40))
  280.       return true;
  281.   var searchBox = document.getElementById("search-box");
  282.   searchBox.focus();
  283.   return true;
  284. }
  285.  
  286. function playNewVideos(event, id) {
  287.   eventURL('action:playNewVideos?id=' + id);
  288.   event.stopPropagation(); // don't want handleSelect to deal with this event
  289.   return false;
  290. }
  291.  
  292. ///////////////////////////////////////////////////////////////////////////////
  293. ///////////////////////////////////////////////////////////////////////////////
  294.  
  295. -->
  296. </script>
  297.